<?php

namespace app\admin\model;

use think\facade\Validate;
use think\Model;


/**
 *
 * Class LoginModel
 * @package app\admin\model
 */
class AdminModel extends BaseModel
{
    protected static $rule = [
        "role" => "require",
        "account" => "require",
    ];
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'admin';

    /**
     * 用管理员名查找管理员信息
     * @param string $account
     * @return mixed
     */
    public function accountByAdmin(string $account)
    {
        return $this->where(['account' => $account, 'status' => 1])->find();
    }

    /**
     * 手机号查找管理员信息
     * @param string $phone
     * @return mixed
     */
    public function mobileByAdmin(string $phone)
    {
        return $this->where(['phone' => $phone, 'status' => 1])->find();
    }

    /**
     * 按条件获取管理员数据
     * @param $where
     * @return mixed
     */
    public function getInfo($where)
    {
        return $this->getModel()->where($where)->find();
    }

    /**
     * 获取满足数据条数
     * @param array $where
     * @return int
     */
    public static function count($where = [])
    {
        return static::where($where)->count();
    }

    /**
     * 编辑用户信息
     * @param array $p
     * @return array
     */
    public static function editAdmin($p = [])
    {

        $p = !empty($p) ? filter_param($p) : [];   //过滤参数
        if (!empty($p['pwd']))
            $p['pwd'] = password_hash($p['pwd'], PASSWORD_DEFAULT);
//        $validate =  Validate::rule(static::$rule);
//        if(!$validate->check($p)) return ['code' => 50001 , "msg" => $validate->getError()];
        if (!empty($p['id'])) {
            $res = static::update($p, ['id' => $p['id']]);
        } else {
            $res = static::create($p);
        }
        return ['code' => $res ? 200 : 50001, "msg" => $res ? "操作成功" : "操作失败"];
    }

    /**
     * 删除用户
     * @param $id
     * @return array
     */
    public static function del($ids)
    {
        $res = static::where('id', 'in', $ids)->delete();
        return ['code' => $res ? 200 : 50001, "msg" => $res ? "操作成功" : "操作失败"];
    }

    /**
     * 最后一次修改密码的时间
     * @param int $uid
     * @return mixed
     */
    public function pwdExpire(int $uid)
    {
        return static::where("id", $uid)->value("last_edit_pwd");
    }

    public static function updatePwd($id, $old, $new)
    {
        $adminInfo = self::find($id);
        if (!password_verify($old, $adminInfo->pwd)) {
            api_return(20004);
        }
        $adminInfo->pwd = password_hash($new, PASSWORD_DEFAULT);
        $res = $adminInfo->save();
        api_return(($res) ? 200 : 50001);

    }


    public static function onBeforeUpdate(Model $model)
    {
        if(self::where([
            ['phone','=',$model->phone],
            ['role','=',$model->role],
            ['id','<>',$model->id],
        ])->find())
        {
            api_return(500,'同角色电话号码已存在');
        }
        if(self::where([
            ['account','=',$model->account],
            ['id','<>',$model->id],
        ])->find())
        {
            api_return(500,'用户名已存在');
        }
        if($model->phone!=self::find($model->id)->phone)
        {
           $model->openid='';
        }
    }
    public static function onBeforeInsert(Model $model)
    {
        if(!$model->pwd){
            api_return(500,'新增用户密码不能为空');
        }
        if(self::where([
            ['phone','=',$model->phone],
            ['role','=',$model->role]
        ])->find())
        {
            api_return(500,'同角色电话号码已存在');
        }
        if(self::where([
            ['account','=',$model->account],
        ])->find())
        {
            api_return(500,'用户名已存在');
        }
    }

    public function order(){
        return $this->hasMany(OrderInModel::class);
    }
}
